package br.edu.ufcg.lsd.wbis.ui.client.ui.setup;

import br.edu.ufcg.lsd.wbis.ui.client.beans.UserInfo;
import br.edu.ufcg.lsd.wbis.ui.client.services.SetupService;
import br.edu.ufcg.lsd.wbis.ui.client.ui.management.PanelManager;

import com.gwtext.client.core.EventObject;
import com.gwtext.client.widgets.Button;
import com.gwtext.client.widgets.MessageBox;
import com.gwtext.client.widgets.event.ButtonListenerAdapter;

/**
 * @author Renato Miceli
 */
public class CreateRootUserListener extends ButtonListenerAdapter {

	private final AddRootFormPanel parser;

	private final PanelManager panelManager;

	public CreateRootUserListener(AddRootFormPanel parser, PanelManager panelManager) {
		this.parser = parser;
		this.panelManager = panelManager;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void onClick(Button button, EventObject e) {
		final UserInfo info = this.parser.parseForm();
		if (!info.isLoginValid()) {
			MessageBox.alert("Error", "Login must not be empty!");
			return;
		}
		if (!info.isPasswordValid()) {
			MessageBox.alert("Error", "Password must not be empty!");
			return;
		}
		if (!info.isPasswordRetyped()) {
			MessageBox.alert("Error", "The retyped password does not match!");
			return;
		}
		MessageBox.confirm("Confirm", "Are you sure? ", new MessageBox.ConfirmCallback() {
			public void execute(String btnID) {
				if (btnID.equals("yes")) {
					SetupService.Util.getInstance().createRootUser(info, new CreateRootUserCallback(panelManager));
				}
			}
		});
	}

}
